How do I Dynamically Populate the Choices for a List Field?
Description
List fields can be dynamically populated when opened using the ON *editor event.
The list field can be populated dynamically in the ON *editor event for the List. The event is passed an object that represents the List field. You can retrieve the object using the args() function:
listChoices = args(1)
The object has a property, qList, that defines what choices are shown for the List. qList is an array of objects that contain the following properties:
- text
The text to display in the choice list.
- value
An optional value for the displayed choices. The value can be different from the text shown to the user.
For example, the code below shows how to populate a List with a selection of colors:
ON *editor_colors listChoices = args(1) colors = array() arrayPush(colors, obj("text","Red")) arrayPush(colors, obj("text","Orange")) arrayPush(colors, obj("text","Yellow")) arrayPush(colors, obj("text","Green")) arrayPush(colors, obj("text","Blue")) arrayPush(colors, obj("text","Purple")) listChoices.qList = colors ENDON
The optional value property can be used to set a stored value different from the displayed value. This lets you create a list where the displayed value is a Customer's but the stored value is their customer ID. For example, the code below retrieves a list of customers from a SQLite database from the on-device assets. It then creates a list of choices where the displayed value (text) is the Contact Name and the stored value (value) is the Customer ID.
ON *editor_Customers ' Get an object for the field's list editor s = args(1) db = "SQLiteNorthwind.db" sql = "SELECT CustomerID, ContactName FROM customers" sqlresults = phoneGapTFExecuteSQL(db, sql) choices = array() if sqlresults.length<1 arrayPush(choices, obj("value","No customers")) s.qList = choices return ' No query results endif FOR i=0 TO len(sqlresults)-1 row = sqlresults[i] name = row.ContactName id = row.CustomerID ' Add the Contact Name to the list of choices arrayPush(choices, obj("text",name,"value",id)) ENDFOR s.qList = choices ENDON